package org.brisling.oa.queryService;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletResponse;

import org.brisling.authorize.shiro.ShiroSecurityService;
import org.brisling.base.domain.ChartDomain;
import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.oa.domain.OACarApply;
import org.brisling.oa.domain.OAGeneral;
import org.brisling.oa.domain.OAGreenChannel;
import org.brisling.oa.queryService.base.AbstractOAQueryService;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * <p>Title: QueryOAGreenChannelService</p>
 * <p>Description: 绿色通道审批查询服务类</p>
 * <p>Company: 长沙东勤信息科技发展有限公司</p>
 * @author jackson wang
 * @since 1.0
 * @version 1.0
 * @date 2018年3月2日 下午3:23:51
 */
@RestController
//映射路径
@RequestMapping("/oaGreenChannel/query")
                                                                  //实体泛型类型
public class QueryOAGreenChannelService extends AbstractOAQueryService<OAGreenChannel> {
	
	
	
	@Autowired
	private	ShiroSecurityService shiroSecurityService;
	@Autowired
	private QueryDictionaryService dicService;
	
	private static final String DEPT_CODE = "apply_deptid";
	
	private static final String QUERY_BIZ_ALL_CODE = "query_oa_all";
	
	//导出excel抬头
	private String exportTitle = "绿色通道申请记录";
	
	//模块业务编码
	private String moduleCode = "oaGreenChannel";
	
	private String printName = "绿色通道审批单";
	
	//导出excel配置项名称
	private String exportConfig = "oa_greenChannel_export";
	
	public QueryOAGreenChannelService(){
		this.moduleName = ConstSysParam.MODULE_GREEN_CHANNEL;
		this.subSystem = ConstSysParam.SUBSYSTEM_OA;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.moduleName;
		this.clazz = OAGreenChannel.class;
		
		typeMap = this.getTypeMap(clazz);
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
		
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<OAGreenChannel> getListDomain(
			@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getOperateTaskListDomain.action")
	public ListDomain<OAGreenChannel> getUserCanoperateTask(@RequestBody(required=false) String body){
		return super.getUserCanoperateTask(body);
	}
	
	@RequestMapping("/getSignedTaskListDomain.action")
	public ListDomain<OAGreenChannel> getUserSignedTask(@RequestBody(required=false) String body){
		return super.getUserSignedTask(body);
	}
	
	@RequestMapping("/getUnsignedTaskListDomain.action")
	public ListDomain<OAGreenChannel> getUserUnsignedTask(@RequestBody(required=false) String body){
		return super.getUserUnsignedTask(body);
	}
	
	
	@RequestMapping("/getFinishedTaskListDomain.action")
	public ListDomain<OAGreenChannel> getUserFinishedTask(@RequestBody(required=false) String body){
		return super.getUserFinishedTask(body);
	}
	
	@RequestMapping("/getUserManagedTaskDomain.action")
	public ListDomain<OAGreenChannel> getUserManagedTask(@RequestBody(required=false) String body){
		return super.getUserManagedTask(body);
	}
	
	@RequestMapping("/getAllTaskListDomain.action")
	public ListDomain<OAGreenChannel> getAllTask(@RequestBody(required=false) String body){
		return super.getAllTask(body);
	}
	
	@RequestMapping("/getTaskListDomain.action")
	public ListDomain<OAGreenChannel> getTaskListDomain(@RequestBody(required=false) String body){
		return super.getTaskListDomain(body);
	}
	
	/**
	 * 获取任务数量
	 * @return
	 */
	@RequestMapping("/getTaskCnt.action")
	public List<ChartDomain> getTaskCnt(@RequestBody(required=false) String body){
		
		List<ChartDomain> domains = new ArrayList<ChartDomain>();
		String issue = "medical";
		Long canOpera = null;
		Long managed = null;
		Long finished = null;
		ListDomain<OAGreenChannel> inquirys = this.getUserCanoperateTask(null);
		
		canOpera = inquirys.getTotal();
		ChartDomain operate = new ChartDomain("待处理",issue,canOpera);
		domains.add(operate);
		
		inquirys = this.getUserManagedTask(null);
		managed = inquirys.getTotal();
		ChartDomain manage = new ChartDomain("处理中",issue,managed);
		domains.add(manage);
		
		inquirys = this.getUserFinishedTask(body);
		finished = inquirys.getTotal();
		ChartDomain finish = new ChartDomain("已处理",issue,finished);
		domains.add(finish);
		
		return domains;
		
	}
	
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public OAGreenChannel getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public OAGreenChannel getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	/**
	 * 根据指定字段名及字段值查找记录
	 * @param field		字段名
	 * @param fieldVal	字段值
	 * @return
	 */
	@RequestMapping("/getOneByField.action")
	public OAGreenChannel getOneByField(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByIdentify(field,fieldVal);
	}
	
	/**
	 * 根据指定字段名及字段值查找记录
	 * @param field		字段名
	 * @param fieldVal	字段值
	 * @return
	 */
	@RequestMapping("/getOneByBizTable.action")
	public OAGreenChannel getOneByBizTable(@RequestParam(value="field",required=false) String field,
			@RequestParam(value="fieldVal",required=false) String fieldVal){	
		return super.getOneByTable(field,fieldVal);
	}
	
	/**
	 * 返回流程记录流程实例的执行记录
	 * @param body	包含procInstId在内的参数信息
	 * @return
	 */
	
	@RequestMapping("/flowHistoricsListDomain.action")
	public ListDomain<Object> getHistoricByProcInstId(@RequestBody(required=false) String body){
		
		String sql = dicService.getSqlByBizcode("flow_historics");				
		return super.getHistoricByProcInstId(body, sql);
	}
	
	public String generateBizno(){
		return super.generateBizno();
	}
	/**
	 * 查询窗口满意度异常数据
	 * @return
	 *
	 */	
	
	public List<OACarApply> getWindowUnSatisfy(){		
		
		String sql =dicService.getSqlByBizcode("duties_abnormal_OACarApply");		
		
		try {
			return super.getClassedAllList(sql, OACarApply.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 	
		return null;
	}

	public List<OAGreenChannel> getAllList(String sql){
		return super.getAllList(sql);
	}
	
	/**
	 * 导出excel文档
	 * @param body
	 * @param response
	 */
	@RequestMapping("/exportExcel.action")
	public void exportExcel(@RequestBody(required=false) String body,HttpServletResponse response){
		super.exportExcel(body, response, exportTitle,exportConfig);
	}
	
	/**
	 * 导出审批完成的excel文档
	 * @param body
	 * @param response
	 */
	@RequestMapping("/exportExcelFinished.action")
	public void exportExcelFinished(@RequestBody(required=false) String body,HttpServletResponse response){
		super.exportExcelFinished(body, response, exportTitle,"注意：当前只能导出已经审批完成的数据",exportConfig);
	}
	
	/**
	 * 导出word表单
	 * @param id	记录id
	 * @return 		导出执行结果 
	 */
	@RequestMapping("/exportWord.action")
	public RetDomain exportWord(@RequestParam(value="id",required=false) Long id,HttpServletResponse response){
		return super.exportWord(id, moduleCode, response,printName);
	}
}
